#ifndef __ALIGN_H
#define __ALIGN_H
#include "stdtype.h"

#if (MX_CPU_BITWIDE == 32)
    #define __N1 1U
#else
    #define __N1 1ULL
#endif

/* align the specified bit(s) and align to top. e.g. align 2 bits: 0b1001 -> 0b1100 */
#define MX_ALIGN_CEIL(var, bitcnt) ((var) = ((var) + ((__N1 << (bitcnt)) - 1)) & ~((__N1 << (bitcnt)) - 1))
/* align the specified bit(s) and align to bottom. e.g. align 2 bits: 0b1001 -> 0b1000 */
#define MX_ALIGN_FLOOR(var, bitcnt) ((var) &= ~((__N1 << (bitcnt)) - 1))
/* clip out the align part. e.g. align 2 bits: 0b1010 -> 0b0010  */
#define MX_ALIGN_CLIP(var, bitcnt) ((var) & ((__N1 << (bitcnt)) - 1))

/* align to cpu's bitwide and align to top. e.g. align 2 bits: 0b1001 -> 0b1100 */
#define MX_ALIGN_CPU_CEIL(var) MX_ALIGN_CEIL(var, MX_CPU_BITWIDE_NUM)
/* align to cpu's bitwide and align to bottom. e.g. align 2 bits: 0b1001 -> 0b1000 */
#define MX_ALIGN_CPU_FLOOR(var) MX_ALIGN_FLOOR(var, MX_CPU_BITWIDE_NUM)
/* clip out the cpu's bitwide align part. e.g. align 2 bits: 0b1010 -> 0b0010  */
#define MX_ALIGN_CPU_CLIP(var) MX_ALIGN_CLIP(var, MX_CPU_BITWIDE_NUM)

#endif
